import os
import paramiko  
from tqdm import tqdm  
  
# SFTP服务器连接信息  
hostname = "120.197.235.112"  
port = 6606  
username = "shanghai_zaixin_sftp"  
password = "s6kg2#F!6ljYF992slj@4"  
remote_path = "/file/upload/"  
local_path = "/data/dev10086/ftpdata/"  
  
# 创建SSH客户端并连接到SFTP服务器  
ssh = paramiko.SSHClient()  
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())  
ssh.connect(hostname, port, username, password)  
sftp = ssh.open_sftp()  
  
# 获取远程目录下的文件名列表  
remote_files = sftp.listdir(remote_path)  
  
# 获取本地已下载的文件名列表（可以根据实际情况修改）  
local_files = os.listdir(local_path)  
  
# 对比文件名，找出尚未下载的文件  
not_downloaded_files = [file for file in remote_files if file not in local_files]  
  
# 下载尚未下载的文件  
for file in tqdm(not_downloaded_files):  
    remote_file_path = remote_path + "/" + file  
    local_file_path = local_path + "/" + file  
    sftp.get(remote_file_path, local_file_path)
  
# 关闭SFTP连接和SSH客户端连接  
sftp.close()
ssh.close()